//
//  YQBottonAlertView.m
//  MuYing
//
//  Created by tbb on 2019/8/26.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "YQBottonAlertView.h"
#import "YQBottonAlertViewCell.h"
#define tableViewHeaderH 60
#define tableViewCellH 58

@interface YQBottonAlertView ()
@property (nonatomic, copy) NSArray * dataSource;
@property (nonatomic, copy) kAlertViewResultBlack resultBlack;
@property (nonatomic, copy) NSString * title;

@property (nonatomic, assign) CGFloat tableviewH;

@property (nonatomic, strong) UIView * bgView;


@end

@implementation YQBottonAlertView

+ (void)alertViewTitle:(NSString *)title
            dataSource:(NSArray *)dataSource
            defaultStr:(NSString *)defaultStr
           resultBlack:(kAlertViewResultBlack)resultBlack {
    YQBottonAlertView *screenView = [[YQBottonAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds title:title dataSource:dataSource defaultStr:defaultStr];
    screenView.backgroundColor = HexColorA(0x000000, 0.2);//HexColor(0xF3F5F9);
    if (resultBlack) {
        screenView.resultBlack = resultBlack;
    }
    [screenView showSelctTableView];
}

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title dataSource:(NSArray *)dataSource defaultStr:(NSString *)defaultStr {
    if (self = [super initWithFrame:frame]) {
        self.title = title;
        self.dataSource = dataSource;
        self.tableviewH = tableViewHeaderH + self.dataSource.count * 58;
        self.bgView.frame = CGRectMake(0, kYQPScreenHeight + self.tableviewH, kYQPScreenWidth, self.tableviewH);
        [self addSubview:self.bgView];
        
        [self.bgView addSubview:self.selectTableView];
        [self.selectTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        self.selectTableView.backgroundColor = UIColor.whiteColor;
        [UIView roundedSetView:self.bgView cornerRadius:12 Type:(kRoundedSetViewTypeTRL)];
        [self.selectTableView registerNib:[UINib nibWithNibName:@"YQBottonAlertViewCell" bundle:nil] forCellReuseIdentifier:@"YQBottonAlertViewCell"];
        
        
        NSInteger index = [dataSource indexOfObject:defaultStr];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self.selectTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:(UITableViewScrollPositionNone)];
    }
    return self;
}

-(void)showSelctTableView {
    //1. 获取当前应用的主窗口
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    // 浮现动画
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.top = kYQPScreenHeight - self.tableviewH-kHeight_Botton_SafeArea;
    }];
    
}

-(void)cancelSelctTableView {
    
    // 关闭动画
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.top = kYQPScreenHeight + self.tableviewH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQBottonAlertViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQBottonAlertViewCell"];
    cell.titleLb.text = self.dataSource[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 58;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.resultBlack) {
        self.resultBlack(indexPath.row,self.dataSource[indexPath.row]);
        [self cancelSelctTableView];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *lb = [[UILabel alloc] init];
    lb.textColor = kAPPMainTitleColor;
    lb.font = [UIFont systemFontOfSize:17 weight:(UIFontWeightMedium)];
    lb.textAlignment = NSTextAlignmentCenter;
    lb.text = self.title;
    return lb;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end


-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = UIColor.whiteColor;

    }
    return _bgView;
}
@end
